/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.image.BufferedImage;

/**
 *
 * @author Marcin Piłat
 */
public class Rotation3DDialog extends javax.swing.JDialog {

    private Rotation3DProcess proc = new Rotation3DProcess();
    private BufferedImage image;
    private ImagePanel imgPanel;
    private AppControler ctrl;
    
    public Rotation3DDialog(java.awt.Frame parent, boolean modal, BufferedImage img, AppControler c) {
        super(parent, modal);
        initComponents();
        ctrl = c;
        imgPanel = ImagePanel.getSmallImagePanel(img);
        image = imgPanel.getImage();
        panel.add(imgPanel);
        setWindowSize(image.getWidth(), image.getHeight());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sliderX = new javax.swing.JSlider();
        labelXa = new javax.swing.JLabel();
        sliderY = new javax.swing.JSlider();
        labelYa = new javax.swing.JLabel();
        sliderZ = new javax.swing.JSlider();
        labelZa = new javax.swing.JLabel();
        panel = new javax.swing.JPanel();
        cameraZ = new javax.swing.JSlider();
        jButton1 = new javax.swing.JButton();
        labelDist = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        sliderSize = new javax.swing.JSlider();
        labelScale = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        sliderX.setMaximum(180);
        sliderX.setMinimum(-180);
        sliderX.setToolTipText("");
        sliderX.setValue(0);
        sliderX.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderXStateChanged(evt);
            }
        });

        labelXa.setText("0");

        sliderY.setMaximum(180);
        sliderY.setMinimum(-180);
        sliderY.setValue(0);
        sliderY.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderYStateChanged(evt);
            }
        });

        labelYa.setText("0");

        sliderZ.setMaximum(180);
        sliderZ.setMinimum(-180);
        sliderZ.setToolTipText("");
        sliderZ.setValue(0);
        sliderZ.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderZStateChanged(evt);
            }
        });

        labelZa.setText("0");

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 267, Short.MAX_VALUE)
        );

        cameraZ.setMaximum(5000);
        cameraZ.setValue(100);
        cameraZ.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cameraZStateChanged(evt);
            }
        });

        jButton1.setText("Rotuj 3D");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        labelDist.setText("0");

        jLabel1.setText("Kąt X");

        jLabel2.setText("Kąt Y");

        jLabel3.setText("Odległość");

        jLabel4.setText("Kąt Z");

        sliderSize.setMaximum(300);
        sliderSize.setMinimum(100);
        sliderSize.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderSizeStateChanged(evt);
            }
        });

        labelScale.setText("1.0");

        jLabel6.setText("Skala");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(12, 12, 12)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel4)))
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(sliderX, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, 0)
                                        .addComponent(labelXa, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(sliderY, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, 0)
                                        .addComponent(labelYa, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(sliderZ, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(cameraZ, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
                                        .addGap(0, 0, 0)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(labelDist, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(labelZa, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sliderSize, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, 0)
                                .addComponent(labelScale, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelXa, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sliderX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelYa, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sliderY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelZa, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sliderZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelDist, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cameraZ, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelScale, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sliderSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jButton1)
                .addGap(0, 0, 0)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ctrl.setProcess(proc);
        ctrl.process();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void sliderXStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderXStateChanged
        double x = sliderX.getValue();
        labelXa.setText("" + x);
        proc.setxAngle(x);
        makeLookahead();
    }//GEN-LAST:event_sliderXStateChanged

    private void sliderYStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderYStateChanged
        double y = sliderY.getValue();
        labelYa.setText("" + y);
        proc.setyAngle(y);
        makeLookahead();
    }//GEN-LAST:event_sliderYStateChanged

    private void sliderZStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderZStateChanged
        double z = sliderZ.getValue();
        labelZa.setText("" + z);
        proc.setzAngle(z);
        makeLookahead();
    }//GEN-LAST:event_sliderZStateChanged

    private void cameraZStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cameraZStateChanged
        double z = cameraZ.getValue();
        labelDist.setText("" + z);
        proc.setCameraZ(z);
        makeLookahead();
    }//GEN-LAST:event_cameraZStateChanged

    private void sliderSizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderSizeStateChanged
        double s = sliderSize.getValue();
        s /= 100;
        labelScale.setText("" + s);
        proc.setScale(s);
        makeLookahead();
    }//GEN-LAST:event_sliderSizeStateChanged

    private void makeLookahead(){
        BufferedImage tmpImage = proc.process(image);
        imgPanel.setImage(tmpImage);
        setWindowSize(tmpImage.getWidth(), tmpImage.getHeight());
        //imgPanel.setSize(tmpImage.getWidth(), tmpImage.getHeight());
    }
    
    private void setWindowSize(int w, int h){
        int ow = getWidth();
        int oh = getHeight();
        int nw, nh;
        w+=30;
        h+=50;
        if(ow < w ){
            nw = w;
        }else{
            nw = ow;
        }
        if(oh < h ){
            nh = h;
        }else{
            nh = oh;
        }
        setSize(nw, nh);
        imgPanel.setSize(w, h);
        panel.setSize(w, h);
    }
    
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider cameraZ;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel labelDist;
    private javax.swing.JLabel labelScale;
    private javax.swing.JLabel labelXa;
    private javax.swing.JLabel labelYa;
    private javax.swing.JLabel labelZa;
    private javax.swing.JPanel panel;
    private javax.swing.JSlider sliderSize;
    private javax.swing.JSlider sliderX;
    private javax.swing.JSlider sliderY;
    private javax.swing.JSlider sliderZ;
    // End of variables declaration//GEN-END:variables
}
